package com.ocom.aiotweb.controller;

import com.ocom.aiotweb.service.UserPageService;
import com.ocom.aiotweb.vo.userpage.AddUserVo;
import com.ocom.aiotweb.vo.userpage.DeleteVo;
import com.ocom.aiotweb.vo.userpage.OnShowVo;
import com.ocom.common.constant.AiotUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
public class UserPageController {

    private final Logger log = LoggerFactory.getLogger(MinioController.class);

    @Autowired
    private UserPageService userPageService;

    // 添加用户（UserPage/addUser）
    @PostMapping(AiotUrl.URL_Aiot_UserPageAddUser)
    public Result UserPageAddUser(@Validated @RequestBody AddUserVo addUserVo){
        if (addUserVo.getStatus()==null){
            log.error("启用状态为空");
            return ResultUtil.error(ResultEnum.Err_203,"启用状态为空");
        }
        return userPageService.UserPageAddUser(addUserVo);
    }

    //修改密码（UserPage/modify）
    @PostMapping(AiotUrl.URL_Aiot_UserPageModify)
    public Result UserPageModify(@Validated @RequestBody DeleteVo deleteVo){
        if (deleteVo.getPassword().isEmpty()){
            return ResultUtil.error(ResultEnum.Err_203,"password");
        }
        return userPageService.UserPageModify(deleteVo.getPassword());
    }
    
    // 用户列表（UserPage/onShow）
    @PostMapping(AiotUrl.Url_Aiot_UserPageOnShow)
    public Result UserPageOnShow(@Validated @RequestBody OnShowVo onShowVo){
        if (onShowVo.getPage() == null){
            log.error("页码page为空");
            return ResultUtil.error(ResultEnum.Err_203,"页码page为空");
        }
        if (onShowVo.getSize() == null){
            log.error("页数size为空");
            return ResultUtil.error(ResultEnum.Err_203,"页数size为空");
        }
        return userPageService.UserPageOnShow(onShowVo);
    }

    //用户角色列表（UserPage/roleInfo）
    @GetMapping(AiotUrl.Url_Aiot_UserPageRoleInfo)
    public Result UserPageRoleInfo(Long userId){
        if (userId == null){
            log.error("用户id为空");
            return ResultUtil.error(ResultEnum.Err_203,"用户id为空");
        }
        return userPageService.UserPageRoleInfo(userId);
    }
}
